package com.czz.ucenter.utils;

import com.czz.ucenter.config.JwtProperties;
import com.czz.ucenter.entity.Admin;
import com.czz.ucenter.entity.Teacher;
import com.czz.ucenter.entity.User;
import io.jsonwebtoken.*;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;

public class JwtUtils {

	public static final String USERSUBJECT = JwtProperties.USERSUBJECT;
 	public static final String TEACHERSUBJECT = JwtProperties.TEACHERSUBJECT;
	public static final String ADMINSUBJECT = JwtProperties.ADMINSUBJECT;

	//秘钥
	public static final String APPSECRET = JwtProperties.APPSECRET;

	//过期时间，毫秒
	public static final long USEREXPIRE = JwtProperties.USEREXPIRE*60*1000;
	public static final long MANAGEEXPIRE = JwtProperties.MANAGEEXPIRE*60*1000;

	/**
	 * 生成用户Jwt令牌
	 * @param user
	 * @return
	 */
	public static String generateJWT(User user){

		if (user == null || StringUtils.isEmpty(user.getId())
				|| StringUtils.isEmpty(user.getMobile())
				|| StringUtils.isEmpty(user.getNickname())
				|| StringUtils.isEmpty(user.getAvatar())
		) {
			return null;
		}

		String token = Jwts.builder()
				.setSubject(USERSUBJECT)
				.claim("id", user.getId())
				.claim("mobile",user.getMobile())
				.claim("name", user.getNickname())
				.claim("avatar", user.getAvatar())
				.setIssuedAt(new Date())
				.setExpiration(new Date(System.currentTimeMillis() + USEREXPIRE))
				.signWith(SignatureAlgorithm.HS256, APPSECRET).compact();

		return token;
	}

	/**
	 * 生成讲师Jwt令牌
	 * @param user,teacher
	 * @return
	 */
	public static String generateJWT(User user, Teacher teacher){

		if (user == null || StringUtils.isEmpty(teacher.getTeacherID())
				|| StringUtils.isEmpty(user.getAvatar())
				|| StringUtils.isEmpty(user.getMobile())
				|| StringUtils.isEmpty(teacher.getName())
		) {
			return null;
		}

		JwtBuilder builder = Jwts.builder();
		builder.setSubject(TEACHERSUBJECT);
		builder.claim("id", teacher.getTeacherID());
		builder.claim("mobile", user.getMobile());
		builder.claim("name", teacher.getName());
		builder.claim("avatar", user.getAvatar());
		builder.setIssuedAt(new Date());
		builder.setExpiration(new Date(System.currentTimeMillis() + MANAGEEXPIRE));
		builder.signWith(SignatureAlgorithm.HS256, APPSECRET);
		String token = builder.compact();

		return token;
	}

	/**
	 * 生成管理员jwt令牌
	 * @param admin
	 * @return
	 */
	public static String generateJWT(Admin admin){

		if (admin == null || StringUtils.isEmpty(admin.getId())
				|| StringUtils.isEmpty(admin.getMobile())
				|| StringUtils.isEmpty(admin.getName())
		) {
			return null;
		}

		JwtBuilder builder = Jwts.builder();
		builder.setSubject(ADMINSUBJECT);
		builder.claim("id", admin.getId());
		builder.claim("mobile", admin.getMobile());
		builder.claim("name", admin.getName());
		builder.setIssuedAt(new Date());
		builder.setExpiration(new Date(System.currentTimeMillis() + MANAGEEXPIRE));
		builder.signWith(SignatureAlgorithm.HS256, APPSECRET);
		String token = builder.compact();

		return token;
	}

	/**
	 * 校验jwt
	 * @param jwtToken
	 * @return
	 */
	public static Claims checkJWT(String jwtToken){

		Jws<Claims> claimsJws = Jwts.parser().setSigningKey(APPSECRET).parseClaimsJws(jwtToken);
		Claims claims = claimsJws.getBody();

		return claims;
	}
}
